#!/bin/bash

# 将脚本运行中过程生成的文件集中存放
BASE_DIR=$(pwd)
TEMP_DIR=$BASE_DIR/init_debian
if [ ! -d $TEMP_DIR ];then
    mkdir -p $TEMP_DIR
fi

repeat() {
    while :
    do
        $@ && return
        sleep 5
    done
}

# 系统更新地址切换到国内
step1_update_mirrors() {
    echo "1 => 系统更新地址切换到国内......"
    source_address=$(cat /etc/apt/sources.list | grep -v "#" | grep "deb " | awk 'NR==1{print}' | awk -F " " '{print $2}')
    echo $source_address
    if [ $source_address == "http://deb.debian.org/debian" ];then
        mirrors_type=-1
        while [ $mirrors_type -eq -1 ];do
            read -p"选择将系统更新地址切换到国内哪个镜像源:1. 清华源;2. 阿里源;3. 华为源;4. 科大源;5. 不切换。" mirrors_type
            if [ $mirrors_type -eq 1 ];then
                # 清华源
                sed -e "s@http://ftp.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g"\
                    -e "s@http://security.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g"\
                    -e "s@http://deb.debian.org@https://mirrors.tuna.tsinghua.edu.cn@g"\
                    -i.bak /etc/apt/sources.list
            elif [ $mirrors_type -eq 2 ];then
                # 阿里源
                sed -e "s@http://ftp.debian.org@https://mirrors.aliyun.com@g"\
                    -e "s@http://security.debian.org@https://mirrors.aliyun.com@g"\
                    -e "s@http://deb.debian.org@https://mirrors.aliyun.com@g"\
                    -i.bak /etc/apt/sources.list
            elif [ $mirrors_type -eq 3 ];then
                # 华为源
                sed -e "s@http://ftp.debian.org@https://repo.huaweicloud.com@g"\
                    -e "s@http://security.debian.org@https://repo.huaweicloud.com@g"\
                    -e "s@http://deb.debian.org@https://repo.huaweicloud.com@g"\
                    -i.bak /etc/apt/sources.list
            elif [ $mirrors_type -eq 4 ];then
                # 科大源
                sed -e "s@http://ftp.debian.org@https://mirrors.ustc.edu.cn@g"\
                    -e "s@http://security.debian.org@https://mirrors.ustc.edu.cn@g"\
                    -e "s@http://deb.debian.org@https://mirrors.ustc.edu.cn@g"\
                    -i.bak /etc/apt/sources.list
            elif [ $mirrors_type -eq 5 ];then
                # 不切换
                let mirrors_type=5
            else
                let mirrors_type=-1
            fi
        done
        apt-get update
    fi
    echo "1 => 系统更新地址切换到国内......OK"
}

# ssh 本机免密
step2_set_ssh_quicklogin() {
    if [ -d $BASE_DIR/.ssh ] && [ -f $BASE_DIR/.ssh/id_rsa ] && [ -f $BASE_DIR/.ssh/id_rsa.pub ];then
        echo "2 => ssh 本机登录免密......"
        if [ ! -d ~/.ssh ];then
            mkdir -p ~/.ssh/
        fi
        cp $BASE_DIR/.ssh/id_rsa* ~/.ssh/
        if [ ! -f /usr/sbin/ifconfig ];then
            apt-get install -y net-tools
        fi
        read -p "请输入需要 ssh 免密登录的 IP 地址:" ip
        ssh-copy-id -i ~/.ssh/id_rsa.pub "$ip"
        echo "2 => ssh 本机登录免密......OK"
    else
        echo "请将可免密登录 ssh 私钥复制到 $BASE_DIR/.ssh 目录下!"
    fi
}

# zsh
step3_install_zsh() {
    echo "3 => 安装 zsh......"
    if [ ! -d ~/.oh-my-zsh ];then
        apt-get install -y zsh git curl wget
        cd $BASE_DIR && sh install_zsh.sh
        #sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
    fi
    cp yhw-miracle.zsh-theme ~/.oh-my-zsh/themes/
    cp ~/.zshrc ~/.zshrc.copy && cp .zshrc ~/

    if [ ! -d $TEMP_DIR/fonts ];then
        cd $TEMP_DIR
        git clone https://gitee.com/yhw-miracle/fonts.git && cd fonts && sh install.sh
        #git clone https://github.com/powerline/fonts.git && cd fonts && sh install.sh
    fi
    echo "3 => 安装 zsh......OK"
}

# vim

step4_install_and_config_vim() {
    echo "4 => 安装并配置 vim......"
    cd $TEMP_DIR
    apt-get install -y vim
    #if [ ! -d $TEMP_DIR/config_vim ];then
    #    # 有可能会下载失败
    #    git clone https://gitee.com/yhw-miracle/config_vim.git --recursive && cd config_vim
    #else
    #    cd config_vim && git pull && git submodule init && git submodule update
    #fi
    cd $BASE_DIR/config_vim
    cp -r .vim ~/
    cp -r vimcolorschemes/* ~/.vim/plugged && cp -r vimplugins/* ~/.vim/plugged
    cp ubuntu/.vimrc ~/
    echo "4 => 安装并配置 vim......OK"
}

# docker
step5_install_and_config_docker() {
    read -p "是否安装 docker(0:不安装; 1:安装):" is_install_docker
    if [ ! -f /usr/bin/docker ] && [ $is_install_docker -eq 1 ];then
        echo "5 => 安装并配置 docker......"
        cd $TEMP_DIR
        curl -fsSL https://get.docker.com -o get-docker.sh
        #public key containerd.io.1.5.11-3.1.el7.x86_64.rpm is not installed
        #cd $TEMP_DIR
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.5.11-3.1.el7.x86_64.rpm && yum install -y containerd.io-1.5.11-3.1.el7.x86_64.rpm
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-cli-20.10.14-3.el7.x86_64.rpm && yum install -y docker-ce-cli-20.10.14-3.el7.x86_64.rpm
        #wget https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-20.10.14-3.el7.x86_64.rpm && yum install -y docker-ce-20.10.14-3.el7.x86_64.rpm
        sh get-docker.sh
    fi

    if [ -f /usr/bin/docker ];then
        if [ ! -d /etc/docker ];then
            mkdir -p /etc/docker
        fi
        docker_daemon_file=/etc/docker/daemon.json
        if [ ! -f $docker_daemon_file ];then
            touch $docker_daemon_file
        fi
        if [ ! -d /data/docker_home ];then
            mkdir -p /data/docker_home
        fi
        echo '{' > $docker_daemon_file
        echo '    "registry-mirrors": ["https://bpt6ffci.mirror.aliyuncs.com"],' >> $docker_daemon_file
        echo '    "data-root": "/data/docker_home"' >> $docker_daemon_file
        echo '}' >> $docker_daemon_file
        docker_status=$(systemctl status docker.service | grep Active | awk -F " " '{print $3}')
        if [ $docker_status == "(running)" ];then
            systemctl restart docker.service
        elif [ $docker_status == "(dead)" ];then
            systemctl start docker.service
        fi

        # docker-compose
        cp $BASE_DIR/docker/docker-compose /usr/local/bin/docker-compose && chmod +x /usr/local/bin/docker-compose

        echo "5 => 安装并配置 docker......OK"
    fi
}

step1_update_mirrors
step2_set_ssh_quicklogin
step3_install_zsh
step4_install_and_config_vim
step5_install_and_config_docker

